<table>
    <tr>
        <td>
            <div id="funfacts">
            </div>
        </td>
    </tr>
    <tr>
        <td>
            <link rel="stylesheet" href="tabstyle.css">
            </link>
            <script src="./js/json2.js" type="text/javascript">
            </script>
        </td>
    </tr>
    <tr>
        <td>
            <script type="text/javascript">
            <!--
            var returnedJson = new Array();
            
            function getXmlHttp(){
                if (window.XMLHttpRequest) {
                    xmlhttp = new XMLHttpRequest();
                }
                else 
                    if (window.ActiveXObject) {
                        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                if (xmlhttp == null) {
                    alert("Your browser does not support XMLHTTP.");
                }
                return xmlhttp;
            }
            
            function callback(oo){
                var query = new Array();
                returnedJson.push(query);
                returnedJson.query = JSON.parse(oo);
                var stop_here = 0;
            }
            
            /*
             var fname = "./JSON/prova.txt"
             var xmlhttp = getXmlHttp();
             xmlhttp.onreadystatechange = function(){
             if (xmlhttp.readyState == 4) {
             callback(xmlhttp.responseText);
             }
             }
             xmlhttp.open("GET", fname, true);
             xmlhttp.overrideMimeType("application/json");
             xmlhttp.send(null);
             */
            function object(o){
                function F(){
                }
                F.prototype = o;
                return new F();
            }
            
            function cbfunc(o){
                //o = object(returnedJson);
                var facts = document.getElementById('funfacts');
                facts.className = 'js';
                if (facts) {
                    var data = o.query.results.tr[1].td[1].p;
                    var out = document.createElement('p');
                    out.className = 'fact';
                    facts.insertBefore(out, facts.lastChild);
                    function seed(){
                        //out.innerHTML = data.content;
                        out.innerHTML = data;
                    }
                    //keys = o.query.results.tr;
                    //data = ConvertJsonToTable(o.query.results.tr, keys, 'funfacts', 'tabstyle');
                    //out.innerHTML = data;
                    seed();
                }
            }
            
			function callYQL(){
				var a = 2;
				var b = 1;
				//Oxford
				//var filename = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fwww.sbs.ox.ac.uk%2Fdegrees%2Fmba%2FPages%2Fprocess.aspx%22%20and%20xpath%3D%22%2F%2Ftable%5B%40class%3D'ms-rteTable-1'%5D%2Ftbody%2Ftr%22&format=json&diagnostics=true&callback=cbfunc";
				//LBS
				//var filename = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D'http%3A%2F%2Fwww.london.edu%2Fprogrammes%2Fmba%2Fapplying.html'%20and%20xpath%3D'%2F%2Fdiv%5B%40id%3D%22p8036%22%5D%2Ftable%2Ftbody%2Ftr'&format=json&diagnostics=true&callback=cbfunc";
				//INSEAD
				var filename = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fmba.insead.edu%2Fadmissions%2Fdates_deadlines.cfm%22%20and%20xpath%3D%22%2F%2Fdiv%5B%40id%3D'content'%5D%2Ftable%5B1%5D%2Ftr%22&format=json&diagnostics=true&callback=cbfunc";
				var fileref = document.createElement("script");
				fileref.setAttribute("type", "text/javascript");
				fileref.setAttribute("src", filename);
				if (a > b) {
					if (typeof fileref != "undefined") {
						document.getElementsByTagName("td")[2].appendChild(fileref);
					}
				}
			}
            var NoOfCols;
            var NoOfRows;
            // Funzione string.format
            String.prototype.format = function(){
                var s = this, i = arguments.length;
                
                while (i--) {
                    s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
                }
                return s;
            };
            
            if (!Array.prototype.forEach) {
                Array.prototype.forEach = function(fun /*, thisp*/){
                    var len = this.length;
                    if (typeof fun != "function") 
                        throw new TypeError();
                    
                    var thisp = arguments[1];
                    for (var i = 0; i < len; i++) {
                        if (i in this) 
                            fun.call(thisp, this[i], i, this);
                    }
                };
            }
            
            // Funzione BufferParser
            function bufferParser(BPtdRow, BPtbCon, BPbuffer, NoOfRows, BPj){
                for (ib = 0; ib < NoOfRows; ib++) {
                    if (typeof BPbuffer[BPj][ib] != "undefined") {
                        BPtbCon += BPtdRow.format(BPbuffer[BPj][ib]);
                        delete BPbuffer[BPj][ib];
                        return BPtbCon;
                        break;
                    }
                }
                BPtbCon += BPtdRow.format("&nbsp;");
                return BPtbCon;
            }
            
            // Funzione Add row to buffer
            function addbufferrow(AaRbufferdata, NoOfCols){
                AaRbufferdata.push(new Array(NoOfCols));
                NoOfRows++;
                return AaRbufferdata;
            }
            
            // Funzione Add row to buffer
            function getAttributeByIndex(obj, index){
                var i = 0;
                for (var attr in obj) {
                    if (index === i) {
                        return obj[attr];
                    }
                    i++;
                }
                return null;
            }
            
            // Funzione buildarow
            function buildarow(BaRtdRow, BaRtbCon, BaRjsonData, BaRbufferdata, BaRi, BaRj){
                var NoOfPobjs;
                var Pobj = new Array();
                if (typeof BaRjsonData[BaRi] != "undefined") {
                    if (BaRjsonData[BaRi].td[BaRj].strong) {
                        BaRtbCon += BaRtdRow.format(BaRjsonData[BaRi].td[BaRj].strong);
                    }
                    else 
                        if (typeof BaRjsonData[BaRi].td[BaRj].p != 'undefined') {
                            if (typeof BaRjsonData[BaRi].td[BaRj].p == "object") {
                                NoOfPobjs = BaRjsonData[BaRi].td[BaRj].p.length;
                                if (typeof NoOfPobjs != 'undefined') {
                                    BaRtbCon += BaRtdRow.format(BaRjsonData[BaRi].td[BaRj].p[0]);
                                    for (k = 1; k < BaRjsonData[BaRi].td[BaRj].p.length; k++) {
                                        BaRbufferdata = addbufferrow(BaRbufferdata, NoOfCols);
                                        BaRbufferdata[BaRj][k] = BaRjsonData[BaRi].td[BaRj].p[k];
                                    }
                                }
                                else {
                                    var valo;
                                    Pobj.push(BaRjsonData[BaRi].td[BaRj].p);
                                    valo = getAttributeByIndex(BaRjsonData[BaRi].td[BaRj].p, 0);
                                    BaRtbCon += BaRtdRow.format(valo);
                                }
                            }
                            else {
                                BaRtbCon += BaRtdRow.format(BaRjsonData[BaRi].td[BaRj].p);
                            }
                        }
                        else {
                            BaRtbCon = bufferParser(BaRtdRow, BaRtbCon, BaRbufferdata, 10, BaRj);
                        }
                }
                else {
                    BaRtbCon = bufferParser(BaRtdRow, BaRtbCon, BaRbufferdata, 10, BaRj);
                }
                return BaRtbCon;
            }
            
            // Funzione conversione JSON to HTLM table
            function ConvertJsonToTable(jsonData, keys, containerId, tableClassName){
                //Patterns for table thead & tbody
                var tbl = "<table border='1' cellpadding='1' cellspacing='1' id='" + containerId + "' class='" + tableClassName + "'>{0}{1}</table>";
                var th = "<thead>{0}</thead>";
                var tb = "<tbody>{0}</tbody>";
                var tr = "<tr>{0}</tr>";
                var thRow = "<th>{0}</th>";
                var tdRow = "<td>{0}</td>";
                var pRow = "<p>{0}</p>";
                var thCon = "";
                var tbCon = "";
                var trCon = "";
                var trMio = "";
                var found = 0;
                var td = new Array();
                
                if (keys && jsonData) {
                
                    NoOfCols = keys[0].td.length;
                    NoOfRows = jsonData.length;
                    
                    //var bufferdata = new Array(keys[0].td[0].length);
                    var bufferdata = new Array(NoOfRows);
                    //for (j = 0; j < NoOfCols; j++) {
                    for (j = 0; j < NoOfRows; j++) {
                        bufferdata[j] = new Array(NoOfCols);
                    }
                    
                    //Creating all table headers
                    for (i = 0; i < NoOfCols; i++) {
                        //aa = keys.td[i].strong;
                        thCon = buildarow(thRow, thCon, keys, bufferdata, 0, i);
                        //thCon += thRow.format(tbCon);
                    }
                    th = th.format(tr.format(thCon));
                    tbCon = "";
                    
                    //Creating all table rows from Json data
                    if (typeof(jsonData[0]) == "object") {
                        //for (i = 0; i < NoOfRows; i++) {
                        // parse rows leaving headers apart: i=1
                        for (i = 1; i < NoOfRows; i++) {
                            for (j = 0; j < NoOfCols; j++) {
                                tbCon = buildarow(tdRow, tbCon, jsonData, bufferdata, i, j);
                            }
                            trCon += tr.format(tbCon)
                            tbCon = "";
                        }
                    }
                    tb = tb.format(trCon);
                    
                    tbl = tbl.format(th, tb);
                    return tbl;
                }
                
                return null;
            }
           
			callYQL();
			
            //-->
            </script>
        </td>
    </tr>
	
	<tr>
		<td>
		</td>
	</tr>
</table>
